import Dialog from '../../component/vant/dialog/dialog';

var app = getApp();
const util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    schools: [],
    index: 0,
    grades: [],
    g_index: 0,
    classes: [],
    c_index: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var page = this;

    app.sendRequest({
      url: app.api.public.public_mechanism_list,
      success: function(res) {
        var schools = [{ id: 0, name: '请选择学校' }];
        var school_list = res.data.list || [];
        school_list.map((school, i) => {
          schools.push({ id: school.id, name: school.name });
        });
        page.setData({
          schools: schools
        });
      }
    });
    app.sendRequest({
      url: app.api.public.public_grade_list,
      success: function(res) {
        var grades = [{ id: 0, grade_name: '请选择年级' }];
        var grade_list = res.data.list || [];
        grade_list.map((grade, i) => {
          grades.push({ id: grade.grade_id, grade_name: grade.grade_name });
        });
        page.setData({
          grades: grades
        });
      }
    });
    var classes = [{ id: 0, class_name: '请选择班级' }];
    for (var i = 1; i < 30; i++) {
      classes.push({ id: i, class_name: util.NumberToChinese(i) + '班' });
    }
    page.setData({
      classes: classes
    });
    page.getPersonalData();
  },
  getPersonalData() {
    let page = this;
    app.sendRequest({
      url: app.api.public.student_info,
      success: function(res) {
        page.setData({
          name: res.data.student_name,
          phone: res.data.mobile
        });
      }
    });
  },
  bindPickerChange: function(e) {
    var page = this;
    var type = e.currentTarget.dataset.type;
    switch (type) {
      case '0':
        page.setData({
          index: e.detail.value,
          school_err: false
        });
        break;
      case '1':
        page.setData({
          g_index: e.detail.value,
          grade_err: false
        });
        break;
      case '2':
        page.setData({
          c_index: e.detail.value,
          class_err: false
        });
        break;
    }
  },
  nameChange(e) {
    var val = e.detail,
      page = this;
    page.setData({
      name: val
    });
  },
  phoneChange(e) {
    var val = e.detail,
      page = this;
    page.setData({
      phone_err: false,
      phone_err_msg: '',
      phone: val
    });
  },
  infoCheck() {
    var page = this;
    let name = page.data.name,
      phone = page.data.phone,
      school_index = page.data.index,
      g_index = page.data.g_index,
      c_index = page.data.c_index;

    //真实姓名
    if (name == undefined || name == '' || name.length == 0) {
      page.setData({
        name_err: true
      });
      return false;
    } else {
      page.setData({
        name_err: false
      });
    }

    //手机号
    var mobile = /^(1[0-9]{10})$/;
    if (!mobile.test(phone)) {
      var phone_err_msg = '手机号格式错误';
      page.setData({
        phone_err: true
      });
      if (phone.length != undefined && phone.length > 0) {
        page.setData({
          phone_err_msg: phone_err_msg
        });
      }
      return false;
    } else {
      page.setData({
        phone_err: false,
        phone_err_msg: ''
      });
    }

    //学校
    if (school_index == 0) {
      page.setData({
        school_err: true
      });
      return false;
    } else {
      page.setData({
        school_err: false
      });
    }

    //年级
    if (g_index == 0) {
      page.setData({
        grade_err: true
      });
      return false;
    } else {
      page.setData({
        grade_err: false
      });
    }

    //班级
    if (c_index == 0) {
      page.setData({
        class_err: true
      });
      return false;
    } else {
      page.setData({
        class_err: false
      });
    }
    return true;
  },
  sureChoose: function() {
    var page = this;

    //基础信息check
    if (page.infoCheck()) {
      let school_index = page.data.index,
        g_index = page.data.g_index,
        c_index = page.data.c_index;

      var params = {
        name: page.data.name,
        mobile: parseInt(page.data.phone),
        mechanism_id: parseInt(page.data.schools[school_index].id),
        grade_id: parseInt(page.data.grades[g_index].id),
        class_name: parseInt(page.data.classes[c_index].id)
      };

      app.sendRequest({
        url: app.api.school.public_student_info_finish,
        data: params,
        method: 'POST',
        header: {
          token: wx.getStorageSync('access_token'),
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {

          wx.switchTab({
            url: '/pages/my/my'
          });
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
